package com.example.datastorage;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    String prefName = "com.example.datastorage";

    private Button btnLogin;
    private Button btnRegis;
    private boolean flag = false;

    private EditText editAge;
    private EditText editSex;
    private EditText editPwd;
    private EditText editName;
    private TextView txtShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        saveDataSharedPreference();

        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnRegis = (Button) findViewById(R.id.btnRegis);
        btnLogin.setOnClickListener(this);
        btnRegis.setOnClickListener(this);

        editAge = (EditText) findViewById(R.id.editAge);
        editSex = (EditText) findViewById(R.id.editSex);
        editPwd = (EditText) findViewById(R.id.editPwd);
        editName = (EditText) findViewById(R.id.editName);
        txtShow = (TextView) findViewById(R.id.txtShow);

    }
    // 页面A登录 B注册 C主页
    //

    private void getUserInfo() {
        // 通过共享首选项获取用户信息
    }

    private void saveUserInfo() {
        // 通过共享首选项保存用户信息
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        int id = v.getId();
        switch (id) {
            case R.id.btnLogin:
                if (!flag) {
                    txtShow.setText("未注册");
                    return;
                }
                // 开启页面
                intent = new Intent(MainActivity.this, MsgActivity.class);
                startActivity(intent);
                break;
            case R.id.btnRegis:
                flag = true;
                // 开启页面
                intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 65);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 65) {
            if ((resultCode == Activity.RESULT_OK)) {
                txtShow.setText("注册成功");
                String user = data.getStringExtra("user");
                String pwd = data.getStringExtra("pwd");
                String age = data.getStringExtra("age");
                String sex = data.getStringExtra("sex");
                editName.setText(user);
                editPwd.setText(pwd);
                editAge.setText(age);
                editSex.setText(sex);
                UserInfo userInfo = new UserInfo();
                userInfo.setUsername(user);
                userInfo.setUsername(pwd);
                userInfo.setAge(Integer.parseInt(age));
                if (sex.equals("男")) {
                    userInfo.setSex(true);
                } else if (sex.equals("女")) {

                    userInfo.setSex(false);
                }
                SharedPreferences userInfo1 = getSharedPreferences("userInfo", MODE_PRIVATE);
                SharedPreferences.Editor edit = userInfo1.edit();
                Set<String> strings = new HashSet<>();
                strings.add("1");
                strings.add(userInfo.getPassword());
                strings.add(userInfo.getUsername());
//                strings.add(userInfo.getPassword());
                edit.putString("user",userInfo.getUsername());
                edit.putStringSet("password",strings);
                edit.putString("age",String.valueOf(userInfo.getAge()));
                edit.putString("sex",String.valueOf(userInfo.getSex()));
                edit.apply();
            }
        }
    }

    class UserInfo {
        private String username;
        private String password;
        private int age;
        private Boolean sex;

        public UserInfo() {
        }

        public UserInfo(String username, String password, int age, Boolean sex) {
            this.username = username;
            this.password = password;
            this.age = age;
            this.sex = sex;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Boolean getSex() {
            return sex;
        }

        public void setSex(Boolean sex) {
            this.sex = sex;
        }
    }

    public void saveDataSharedPreference() {
        SharedPreferences sharedPreferences;
        sharedPreferences = getSharedPreferences(prefName, MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean("bool", false);
        edit.putFloat("float", (float) 3.13);
        edit.putInt("int", 123);
        edit.putString("username", "sjr");
        edit.putString("password", "123456");
        edit.apply();

        String username = sharedPreferences.getString("username", "");
        String password = sharedPreferences.getString("password", "");
    }

    public void saveDataWithFile() {
        // 1.读写内部存储器
        String fileName = "myFile.txt";
//        File file = new File(getFilesDir(), fileName);
//        File file = new File(getCacheDir(), fileName);
        File file = new File(getExternalCacheDir(), fileName);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
//            fileOutputStream.write("Hello World".getBytes());
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
            bufferedWriter.write("hello world");
            bufferedWriter.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 2. 读内部存储的文件
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            byte[] buff = new byte[20];
//            fileInputStream.read(buff);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
            Log.d("TAG", "onCreate: " + bufferedReader.readLine());
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}